﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LPV_POS.mh_pos;
using LPV_POS.mh_pos.mh_inbaocao;
using LPV_POS.Module;
using LPV_POS.Baocao;
using System.Resources;
using System.Globalization;

/**
 * ---31/07/2012--- Sửa hóa đơn sau khi thanh toán
 **/
namespace LPV_POS.In
{
    public partial class Print_InLai_HoaDon : Form
    {
        CultureInfo culture;
        string str_lang = "";
        string DM_BAN_MA = "";
        string DM_KHUVUC_MA = "";
        Boolean bLoadForm = false;
        string BH_PHIEU_MA = "";
        DataRow drMain;
        public static bool inlai;//-------------16/05/2012------------

        #region "Muti language"

        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_banhang6_PhuThu).Assembly);
                lbel_tencongty.Text = rm.GetString("inlaihoadon", culture);
                BtnNV_PhucVu.Text = rm.GetString("nhanvienphucvu", culture);
                BtnNguoiGioiThieu.Text = rm.GetString("nguoigioithieu_1", culture);
                label3.Text = rm.GetString("thang", culture);
                label1.Text = rm.GetString("khuvuc_1", culture);
                btnIn.Text = rm.GetString("in", culture);
                btnSua_Bill.Text = rm.GetString("sua", culture);
                btnThoat.Text = rm.GetString("thoat", culture);
                label2.Text = rm.GetString("sophieu", culture);
                bton_tim.Text = rm.GetString("tim", culture);
                bton_xemtatca.Text = rm.GetString("xemtatca", culture);
                this.COL_OBJID1.Caption = rm.GetString("sophieu", culture);
                this.col_NGAY_GIO_VAO.Caption = rm.GetString("ngay", culture);
                this.col_TONG_TIEN1.Caption = rm.GetString("tongtien", culture);
                this.col_TINH_TIEN_BANG_THE.Caption = rm.GetString("thanhtoan", culture);
                this.col_OBJNAME2.Caption = rm.GetString("mon", culture);
                this.col_DON_GIA.Caption = rm.GetString("gia", culture);
                this.col_SO_LUONG2.Caption = rm.GetString("soluong", culture);
                this.col_TheTT.Caption = rm.GetString("thethanhtoan", culture); // httt, editor: PL, date: 2012.08.10
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion

        private DataTable f_insert_column(DataTable tb)
        {
            DataColumn col = new DataColumn("FLAG", Type.GetType("System.Boolean"));
            tb.Columns.Add(col);
            return tb;
        }
        int iHINH_THUC_BAN_HANG = -1;
        public Print_InLai_HoaDon(string _DM_KHUVUC_MA)
        {
            InitializeComponent();
            culture = CultureInfo.CurrentCulture;
            int.TryParse(Variable.tbBH_CAU_HINH.Rows[0]["HINH_THUC_BAN_HANG_combo"] + "", out iHINH_THUC_BAN_HANG);
            if (iHINH_THUC_BAN_HANG > 0)
            {
                DM_KHUVUC_MA = _DM_KHUVUC_MA;

                // -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -==
                // -== -== -== -== -== -== -== -== -==  DM_KHUVUC2
                // -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -==
                string sSQL = "SELECT OBJID, OBJNAME, GHICHU FROM DM_KHUVUC";
                thuvien othuvien = new thuvien();
                DataTable tbKhuVuc = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
                cboKhuVuc.Datasource = tbKhuVuc;
                othuvien.f_showdataoncombox(cboKhuVuc, tbKhuVuc, "OBJID", "OBJNAME");

                // -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -==
                // -== -== -== -== -== -== -== -== -==  DM_BAN_1
                // -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -==
                sSQL = "SELECT MAKHUVUC, OBJID, OBJNAME FROM DM_BAN WHERE MAKHUVUC LIKE '" + DM_KHUVUC_MA + "'";
                DataTable TB1 = othuvien.SP_SelectDataTable("sp_executesql", new string[] { "@Ssql" }, new object[] { sSQL });
                if (TB1.Rows.Count > 0)
                {
                    DM_KHUVUC_MA = TB1.Rows[0]["MAKHUVUC"] + "";
                    //lbel_tencongty.Text = lbel_tencongty.Text + " (" + TB1.Rows[0]["OBJNAME"] + ")";
                    lbel_tencongty.Tag = TB1.Rows[0]["OBJNAME"];
                }
                //this.ShowData_tbLeft();
            }
            else
            {
                cboKhuVuc.Properties.ReadOnly = true;
                cboBan.Properties.ReadOnly = true;
                this.ShowData_tbLeft();
            }
            bLoadForm = true;
        }
        private void Print_InLai_HoaDon_Load(object sender, EventArgs e)
        {
            cpublic ocpublic = new cpublic();
            str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";

            SetLanguage_banhang(str_lang);
            cboKhuVuc._SetselectedID(DM_KHUVUC_MA);
            lvSUM.Columns.Add("", 200, HorizontalAlignment.Left);
            lvSUM.Columns.Add("", 140, HorizontalAlignment.Right);
            lvSUM.View = View.Details;

            this.ShowData_tbRight_Bottom();//9999
            bLoadForm = true;
        }

        void ShowData_tbLeft()
        {
            string sSQL =
                        "SELECT " +
                        "   a.NGAY_LAM_VIEC, a.THANH_TIEN, a.CK_TIEN, a.VAT_TIEN, a.TTIEP_TIEN, " +
                        "   a.CK_THUCAN_TIEN, a.CK_THUCUONG_TIEN, " +
                        "   a.PHU_THU, a.PHU_THU_KHAC, " +
                        "   a.OBJID, a.TONG_TIEN, a.NGAY_GIO_VAO, a.NGAY_GIO_RA, a.NGUOI_LAP, a.KM_TIEN, " +
                        "   tenKH = " +
                        "   ( " +
                        "       SELECT TOP 1 ISNULL(k.OBJNAME, nk.OBJNAME) " +
                        "       FROM DM_THE T " +
                        "           LEFT JOIN DM_KHACHHANG k ON k.OBJID = t.DM_KHACHHANG_MA " +
                        "           LEFT JOIN DM_NHOMKHACHHANG nk ON t.DM_NHOMKHACHHANG_MA = nk.OBJID " +
                        "       WHERE a.CUSTOMER_ID = T.CODE " +
                        "   ) , " +
                        "   NHAN_VIEN = ISNULL(nv.OBJNAME, dn.OBJNAME), " +
                        "   FLAG = 'False', " +
                        "   DAT_COC = ISNULL(c.DAT_COC, 0), " +
                        "   a.TIEN_DA_NHAN, " +
                        "   b.DM_LOAIDICHVU_MA,  " +
                        "   a.DM_BAN_MA, a.CUSTOMER_ID, " +
                        "   a.TINH_TIEN_BANG_THE, " +
                        "   TINH_TIEN_BANG_THE_ten = (CASE WHEN a.TINH_TIEN_BANG_THE = 1 THEN N'THẺ' ELSE N'TIỀN MẶT' END), " +
                        "   TEN_THE = BT.OBJNAME_THE," +   //---bc theo httt, --editor: PL, date: 2012.07.24
                        "   DM_BAN_TEN = b.OBJNAME " +

                        "FROM BH_PHIEU a " +
                        "   LEFT JOIN DM_BAN b ON a.DM_BAN_MA = b.OBJID " +
                        "   LEFT JOIN DM_DANGNHAP dn ON dn.USERNAME = a.NGUOI_LAP " +
                        "   LEFT JOIN DM_NHANVIEN nv ON dn.MA_NHANVIEN = nv.OBJID " +
                        "   LEFT JOIN DM_BAN_DAT_BAN c ON c.BH_PHIEU_ID = a.OBJID " +
                        "   LEFT JOIN BH_THE BT ON BT.BH_PHIEU_MA = A.OBJID " +         //---bc theo httt, --editor: PL, date: 2012.07.24
                        "WHERE ('" + DM_BAN_MA + "' = '' OR DM_BAN_MA LIKE '" + DM_BAN_MA + "') " +
                        "   AND ('" + DM_KHUVUC_MA + "' = '' OR b.MAKHUVUC LIKE '" + DM_KHUVUC_MA + "') " +
                        "   AND CONVERT(VARCHAR(8), a.NGAY_LAM_VIEC, 112) = '" + dateTimePicker1.Value.ToString("yyyyMMdd") + "' " +
                        "ORDER BY NGAY_GIO_RA DESC";

            DataTable tbMain1 = new thuvien().SP_SelectDataTable("sp_executesql", new string[] { "@sSQL" }, new object[] { sSQL });
            gridControlLeft.DataSource = tbMain1;  //tbMain1;
        }



        //-----------------------------------------

        void ShowData_tbLeft_tatca()
        {
            string sSQL =
                        "SELECT " +
                        "   a.NGAY_LAM_VIEC, a.THANH_TIEN, a.CK_TIEN, a.VAT_TIEN, a.TTIEP_TIEN, " +
                        "   a.CK_THUCAN_TIEN, a.CK_THUCUONG_TIEN, " +
                        "   a.PHU_THU, a.PHU_THU_KHAC, " +
                        "   a.OBJID, a.TONG_TIEN, a.NGAY_GIO_VAO, a.NGAY_GIO_RA, a.NGUOI_LAP, a.KM_TIEN, " +
                        "   tenKH = " +
                        "   ( " +
                        "       SELECT TOP 1 ISNULL(k.OBJNAME, nk.OBJNAME) " +
                        "       FROM DM_THE T " +
                        "           LEFT JOIN DM_KHACHHANG k ON k.OBJID = t.DM_KHACHHANG_MA " +
                        "           LEFT JOIN DM_NHOMKHACHHANG nk ON t.DM_NHOMKHACHHANG_MA = nk.OBJID " +
                        "       WHERE a.CUSTOMER_ID = T.CODE " +
                        "   ) , " +
                        "   NHAN_VIEN = ISNULL(nv.OBJNAME, dn.OBJNAME), " +
                        "   FLAG = 'False', " +
                        "   DAT_COC = ISNULL(c.DAT_COC, 0), " +
                        "   a.TIEN_DA_NHAN, " +
                        "   b.DM_LOAIDICHVU_MA,  " +
                        "   a.DM_BAN_MA, a.CUSTOMER_ID, " +
                        "   a.TINH_TIEN_BANG_THE, " +
                        "   TINH_TIEN_BANG_THE_ten = (CASE WHEN a.TINH_TIEN_BANG_THE = 1 THEN N'THẺ' ELSE N'TIỀN MẶT' END), " +
                        "   TEN_THE = BT.OBJNAME_THE," +   //---bc theo httt, --editor: PL, date: 2012.07.24
                        "   DM_BAN_TEN = b.OBJNAME " +
                        "FROM BH_PHIEU a " +
                        "   LEFT JOIN DM_BAN b ON a.DM_BAN_MA = b.OBJID " +
                        "   LEFT JOIN DM_DANGNHAP dn ON dn.USERNAME = a.NGUOI_LAP " +
                        "   LEFT JOIN DM_NHANVIEN nv ON dn.MA_NHANVIEN = nv.OBJID " +
                        "   LEFT JOIN DM_BAN_DAT_BAN c ON c.BH_PHIEU_ID = a.OBJID " +
                        "   LEFT JOIN BH_THE BT ON BT.BH_PHIEU_MA = A.OBJID  " +
                        "ORDER BY NGAY_GIO_RA DESC";

            DataTable tbMain1 = new thuvien().SP_SelectDataTable("sp_executesql", new string[] { "@sSQL" }, new object[] { sSQL });
            gridControlLeft.DataSource = tbMain1;  //tbMain1;
        }

        //-----------------------------------------

        DataTable ShowData_tbLeft(string _sOBJID)
        {
            string sSQL =
                        "SELECT " +
                        "   a.NGAY_LAM_VIEC, a.THANH_TIEN, a.CK_TIEN, a.VAT_TIEN, " +
                        "   a.CK_THUCAN_TIEN, a.CK_THUCUONG_TIEN, " +
                        "   a.TTIEP_TIEN, " +
                        "   a.PHU_THU, a.PHU_THU_KHAC, " +
                        "   a.OBJID, a.TONG_TIEN, a.NGAY_GIO_VAO, a.NGAY_GIO_RA, a.NGUOI_LAP, a.KM_TIEN, " +
                        "   tenKH = " +
                        "   (  " +
                        "      SELECT TOP 1 ISNULL(k.OBJNAME, nk.OBJNAME) " +
                        "      FROM DM_THE T " +
                        "          LEFT JOIN DM_KHACHHANG k ON k.OBJID = t.DM_KHACHHANG_MA " +
                        "          LEFT JOIN DM_NHOMKHACHHANG nk ON t.DM_NHOMKHACHHANG_MA = nk.OBJID " +
                        "      WHERE a.CUSTOMER_ID = T.CODE " +
                        "   ) ,  " +
                        "   NHAN_VIEN = ISNULL(nv.OBJNAME, dn.OBJNAME), " +
                        "   FLAG = 'False', " +
                        "   DAT_COC = ISNULL(c.DAT_COC, 0), " +
                        "   a.TIEN_DA_NHAN, " +
                        "   b.DM_LOAIDICHVU_MA,  " +
                        "   a.DM_BAN_MA, a.CUSTOMER_ID, " +
                        "   a.TINH_TIEN_BANG_THE, " +
                        "   TINH_TIEN_BANG_THE_ten = (CASE WHEN a.TINH_TIEN_BANG_THE = 1 THEN N'THẺ' ELSE N'TIỀN MẶT' END) " +
                // "   TEN_THE = bt.OBJNAME_THE  "  +
                        "FROM BH_PHIEU a " +
                        "   LEFT JOIN DM_BAN b ON a.DM_BAN_MA = b.OBJID " +
                        "   LEFT JOIN DM_DANGNHAP dn ON dn.USERNAME = a.NGUOI_LAP " +
                        "   LEFT JOIN DM_NHANVIEN nv ON dn.MA_NHANVIEN = nv.OBJID " +
                        "   LEFT JOIN DM_BAN_DAT_BAN c ON c.BH_PHIEU_ID = a.OBJID " +
                // "   RIGHT JOIN BH_THE bt ON bt.BH_PHIEU_MA = a.OBJID " +
                        "WHERE a.OBJID = '" + _sOBJID + "'";

            return new thuvien().SP_SelectDataTable("sp_executesql", new string[] { "@sSQL" }, new object[] { sSQL });

        } // kt 1

        private void f_showdataongridleft_byid(string id_bill)
        {
            string sSQL =
                    "SELECT " +
                    "   a.NGAY_LAM_VIEC, a.THANH_TIEN, a.CK_TIEN, a.VAT_TIEN, " +
                    "   a.CK_THUCAN_TIEN, a.CK_THUCUONG_TIEN, " +
                    "   a.TTIEP_TIEN, " +
                    "   a.PHU_THU, " +
                    "   a.OBJID, a.TONG_TIEN, a.NGAY_GIO_VAO, a.NGAY_GIO_RA, a.NGUOI_LAP, a.KM_TIEN, " +
                    "   tenKH = " +
                    "   (  " +
                    "      SELECT ISNULL(k.OBJNAME, nk.OBJNAME) " +
                    "      FROM DM_THE T " +
                    "          LEFT JOIN DM_KHACHHANG k ON k.OBJID = t.DM_KHACHHANG_MA    " +
                    "          LEFT JOIN DM_NHOMKHACHHANG nk ON t.DM_NHOMKHACHHANG_MA = nk.OBJID     " +
                    "      WHERE a.CUSTOMER_ID = T.CODE   " +
                    "   ) ,  " +
                    "   NHAN_VIEN = dn.OBJNAME, " +
                    "   FLAG = 'False', " +
                    "   DAT_COC = ISNULL(c.DAT_COC, 0), " +
                    "   a.TIEN_DA_NHAN, " +
                    "   DM_BAN_TEN = b.OBJNAME " +
                    "FROM BH_PHIEU a " +
                    "   LEFT JOIN DM_BAN b ON a.DM_BAN_MA = b.OBJID " +
                    "   LEFT JOIN DM_DANGNHAP dn ON dn.USERNAME = a.NGUOI_LAP " +
                    "   LEFT JOIN DM_BAN_DAT_BAN c ON c.DM_BAN_ID = b.OBJID " +
                    "WHERE a.OBJID = '" + id_bill + "'" +

                    " ORDER BY NGAY_GIO_RA DESC";

            DataTable tbMain1 = new thuvien().SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            gridControlLeft.DataSource = tbMain1;
        }

        #region "   ShowData_tbRight"
        void ShowData_tbRight()
        {
            string sTien_Gio = "Tiền giờ";
            if (Variable.tbDM_LOAIDICHVU.Select("isRun = True AND TIEN_PHONG_GIO <> ''").Length > 0)
                sTien_Gio = Variable.tbDM_LOAIDICHVU.Select("isRun = True AND TIEN_PHONG_GIO <> ''")[0]["TIEN_PHONG_GIO"] + "";

            string sSQL = "SELECT a.OBJID, a.MAHH, a.SO_LUONG, a.DON_GIA, a.CHIETKHAU_PHANTRAM, a.CHIETKHAU_TIEN, a.VAT_PHANTRAM, a.VAT_TIEN, a.THANH_TIEN, " +
                          "     OBJNAME = (CASE WHEN A.MAHH LIKE 'Tien_Gio%' THEN N'" + sTien_Gio + "' ELSE b.OBJNAME END), a.GHI_CHU, a.NGAY_TINH_GIO " +
                          "FROM BH_PHIEU_CT a " +
                          "     LEFT JOIN DM_HH b ON a.MAHH = b.OBJID " +
                          "WHERE BH_PHIEU_MA LIKE '" + BH_PHIEU_MA + "' "
                          ;
            DataTable tbMain2 = new thuvien().SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            gridControlRight.DataSource = tbMain2;

            this.ShowData_tbRight_Bottom();
        }

        void ShowData_tbRight_Bottom()
        {
            ///
            try
            {
                lvSUM.Items.Clear();

                float icong = 0;
                float iCK_TIEN = 0;
                float iCK_AN = 0;
                float iCK_UONG = 0;
                //float idoanhthu = 0;
                float ivat = 0;
                float itienTIP = 0;
                float iPhiDichVu = 0;
                float iPHU_THU_KHAC = 0;
                float itongcong = 0;
                float iKhuyenMai = 0;
                float iDAT_COC = 0;
                float iTIEN_DA_NHAN = 0;
                string sThuNgan = "";
                //
                if (drMain != null)
                {
                    float.TryParse(drMain["THANH_TIEN"] + "", out icong);
                    float.TryParse(drMain["CK_TIEN"] + "", out iCK_TIEN);

                    float.TryParse(drMain["CK_THUCAN_TIEN"] + "", out iCK_AN);
                    float.TryParse(drMain["CK_THUCUONG_TIEN"] + "", out iCK_UONG);

                    float.TryParse(drMain["VAT_TIEN"] + "", out ivat);

                    float.TryParse(drMain["TTIEP_TIEN"] + "", out itienTIP);
                    float.TryParse(drMain["PHU_THU"] + "", out iPhiDichVu);
                    float.TryParse(drMain["PHU_THU_KHAC"] + "", out iPHU_THU_KHAC);
                    float.TryParse(drMain["TONG_TIEN"] + "", out itongcong);
                    float.TryParse(drMain["KM_TIEN"] + "", out iKhuyenMai);

                    float.TryParse(drMain["TIEN_DA_NHAN"] + "", out iTIEN_DA_NHAN);
                    float.TryParse(drMain["DAT_COC"] + "", out iDAT_COC);

                    sThuNgan = drMain["NHAN_VIEN"] + "";

                }
                // tohoaichuong
                string str_cong = "";
                string str_giamthucan = "";
                string str_giamthucuong = "";
                string str_giamtongbill = "";
                string str_phidichvu = "";
                string str_khuyenmai = "";
                string str_phuthukhac = "";
                string str_giamgia = "";
                string str_vat = "";
                string str_tientip = "";
                string str_tongcong = "";
                string str_datcoc = "";
                string str_tienkhachdua = "";
                string str_thungan = "";
                switch (str_lang)
                {
                    case "vi-VN":
                        str_cong = "Cộng";
                        str_giamthucan = "Giảm thức ăn";
                        str_giamthucuong = "Giảm thức uống";
                        str_giamtongbill = "Giảm tổng bill";
                        str_phidichvu = "Phí dịch vụ";
                        str_khuyenmai = "Khuyến mãi";
                        str_phuthukhac = "Phụ thu khác";
                        str_giamgia = "Giảm giá";
                        str_vat = "VAT";
                        str_tientip = "Tiền Tip";
                        str_tongcong = "Tổng cộng";
                        str_datcoc = "Đặt cộc";
                        str_tienkhachdua = "Tiền khách đưa";
                        str_thungan = "Thu ngân";
                        break;
                    case "en-US":
                        str_cong = "Sum";
                        str_giamthucan = "Discount food";
                        str_giamthucuong = "Discount drink";
                        str_giamtongbill = "Discount bill";
                        str_phidichvu = "Service charge";
                        str_khuyenmai = "Promotion";
                        str_phuthukhac = "Other surcharge";
                        str_giamgia = "Discount";
                        str_vat = "VAT tax";
                        str_tientip = "Tip";
                        str_tongcong = "Total";
                        str_datcoc = "Curtail deposit";
                        str_tienkhachdua = "Amount custommer’s money";
                        str_thungan = "Cashier";
                        break;
                }

                ListViewItem lvi = new ListViewItem();
                if (iCK_TIEN > 0 || iCK_AN > 0 || iCK_UONG > 0 || ivat > 0 || itienTIP > 0 || iPhiDichVu > 0 || iDAT_COC > 0 || iPHU_THU_KHAC != 0 || iKhuyenMai > 0)
                {
                    #region ...
                    //1.
                    // lvi = new ListViewItem("CỘNG");
                    lvi = new ListViewItem(str_cong);
                    lvi.SubItems.Add(icong.ToString("N0"));
                    lvSUM.Items.Add(lvi);

                    //2.
                    if (iCK_AN > 0)
                    {
                        //                    lvi = new ListViewItem("GIẢM THỨC ĂN");
                        lvi = new ListViewItem(str_giamthucan);
                        lvi.SubItems.Add(iCK_AN.ToString("N0"));
                        lvSUM.Items.Add(lvi);
                    }

                    //2.
                    if (iCK_UONG > 0)
                    {
                        //lvi = new ListViewItem("GIẢM THỨC UỐNG");
                        lvi = new ListViewItem(str_giamthucuong);
                        lvi.SubItems.Add(iCK_UONG.ToString("N0"));
                        lvSUM.Items.Add(lvi);
                    }

                    //2.
                    if (iCK_TIEN > 0)
                    {
                        //                    lvi = new ListViewItem("GIẢM TỔNG BILL");
                        lvi = new ListViewItem(str_giamtongbill);
                        lvi.SubItems.Add(iCK_TIEN.ToString("N0"));
                        lvSUM.Items.Add(lvi);
                    }
                    ///////////////////
                    //3.
                    if (iPhiDichVu > 0)
                    {
                        //lvi = new ListViewItem("PHÍ DỊCH VỤ");
                        lvi = new ListViewItem(str_phidichvu);
                        lvi.SubItems.Add(iPhiDichVu.ToString("N0"));
                        lvSUM.Items.Add(lvi);
                    }

                    //4.
                    if (iKhuyenMai > 0)
                    {
                        //lvi = new ListViewItem("KHUYẾN MÃI");
                        lvi = new ListViewItem(str_khuyenmai);
                        lvi.SubItems.Add(iKhuyenMai.ToString("N0"));
                        lvSUM.Items.Add(lvi);
                    }
                    //5.
                    if (iPHU_THU_KHAC > 0)
                    {
                        //lvi = new ListViewItem("PHỤ THU KHÁC");
                        lvi = new ListViewItem(str_phuthukhac);
                        lvi.SubItems.Add(iPHU_THU_KHAC.ToString("N0"));
                        lvSUM.Items.Add(lvi);

                    }
                    else if (iPHU_THU_KHAC < 0)
                    {
                        //lvi = new ListViewItem("GIẢM GIÁ");
                        lvi = new ListViewItem(str_giamgia);
                        lvi.SubItems.Add(iPHU_THU_KHAC.ToString("N0"));
                        lvSUM.Items.Add(lvi);

                    }
                    //6.
                    if (ivat > 0)
                    {
                        //lvi = new ListViewItem("VAT");
                        lvi = new ListViewItem(str_vat);
                        lvi.SubItems.Add(ivat.ToString("N0"));
                        lvSUM.Items.Add(lvi);

                    }
                    //7.
                    if (itienTIP > 0)
                    {
                        //lvi = new ListViewItem("TIỀN TIP");
                        lvi = new ListViewItem(str_tientip);
                        lvi.SubItems.Add(itienTIP.ToString("N0"));
                        lvSUM.Items.Add(lvi);
                    }
                    #endregion
                }


                //lvi = new ListViewItem("TỔNG CỘNG");
                lvi = new ListViewItem(str_tongcong);
                lvi.SubItems.Add(itongcong.ToString("N0"));
                lvSUM.Items.Add(lvi);

                if (iDAT_COC > 0)
                {
                    //lvi = new ListViewItem("ĐẶT CỌC");
                    lvi = new ListViewItem(str_datcoc);
                    lvi.SubItems.Add(iDAT_COC.ToString("N0"));
                    lvSUM.Items.Add(lvi);
                }

                if (iTIEN_DA_NHAN > 0)
                {
                    //lvi = new ListViewItem("TIỀN KHÁCH ĐƯA");
                    lvi = new ListViewItem(str_tienkhachdua);
                    lvi.SubItems.Add(iTIEN_DA_NHAN.ToString("N0"));
                    lvSUM.Items.Add(lvi);
                }

                //lvi = new ListViewItem("THU NGÂN");
                lvi = new ListViewItem(str_thungan);
                lvi.SubItems.Add(sThuNgan);
                lvSUM.Items.Add(lvi);
                //---31/07/2012---
                #region" Sửa hóa đơn sau khi thanh toán"
                cpublic ocpublic = new cpublic();
                ocpublic.Registry_Set("BH_PhuThu.VAT", ivat == 0);
                ocpublic.Registry_Set("BH_PhuThu.PHIDICHVU", iPhiDichVu == 0);
                ocpublic.Registry_Set("BH_PhuThu.TIP", itienTIP == 0);
                #endregion
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        private void gridViewLeft_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            //if (bLoadForm)
            //{

            drMain = gridViewLeft.GetDataRow(gridViewLeft.FocusedRowHandle);
            if (drMain != null)
                BH_PHIEU_MA = drMain["OBJID"] + "";
            else
                BH_PHIEU_MA = "";

            DataTable tbTMP = this.ShowData_tbLeft(BH_PHIEU_MA);
            if (tbTMP != null)
                if (tbTMP.Rows.Count > 0)
                    drMain = tbTMP.Rows[0];

            this.ShowData_tbRight();
            //}
            if (drMain != null)
            {//radioButton1_TienMat     //---bc theo httt, --editor: PL, date: 2012.07.24
                bLoadForm = false;
                Boolean bTINH_TIEN_BANG_THE = false;
                Boolean.TryParse(drMain["TINH_TIEN_BANG_THE"] + "", out bTINH_TIEN_BANG_THE);
                //radioButton2_The.Checked = bTINH_TIEN_BANG_THE;           //---bc theo httt, --editor: PL, date: 2012.07.24
                //radioButton1_TienMat.Checked = !bTINH_TIEN_BANG_THE;      //---bc theo httt, --editor: PL, date: 2012.07.24
                bLoadForm = true;
            }
            //else
            //{
            //    // loi chuong
            //    Boolean bTINH_TIEN_BANG_THE = false;
            //    if (drMain != null)
            //        Boolean.TryParse(drMain["TINH_TIEN_BANG_THE"] + "", out bTINH_TIEN_BANG_THE);
            //    radioButton2_The.Checked = bTINH_TIEN_BANG_THE;
            //    radioButton1_TienMat.Checked = !bTINH_TIEN_BANG_THE;
            //}
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnThucHien_Click(object sender, EventArgs e)
        {
            inlai = true;
            if (drMain == null)
                return;
            if (BH_PHIEU_MA != "")
            {
                try
                {
                    this.Cursor = Cursors.WaitCursor;
                    thuvien tv = new thuvien();
                    DataTable tb = tv.SP_SelectDataTable("sp_DM_MAYIN_BIENNHAN", null, null);
                    DataView dv = tb.DefaultView;
                    #region "   sTieuDe - sFooter"
                    string sFooter = "";
                    string sTieuDe = "HÓA ĐƠN BÁN HÀNG";
                    DataTable tb_sp_DM_MAYIN_BIENNHAN_CAUHINH = new thuvien().SP_SelectDataTable("sp_DM_MAYIN_BIENNHAN_CAUHINH", new string[] { "@flag" }, new object[] { 4 });
                    if (tb_sp_DM_MAYIN_BIENNHAN_CAUHINH.Rows.Count > 0)
                    {
                        sFooter = tb_sp_DM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["FOOTER"] + "";
                        sTieuDe = tb_sp_DM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["TITLE"] + "";
                    }
                    #endregion
                    int iPrintOut = 0;
                    inlai = true;
                    if (tb.Rows.Count > 0)
                        int.TryParse(tb.Rows[0]["PrintOut"] + "", out iPrintOut);
                    dv.Sort = "COMNAME";
                    int vitri = dv.Find(new object[] { "USB" });
                    int vitri_A4 = dv.Find(new object[] { "A4" });
                    int vitri_A5 = dv.Find(new object[] { "A5" });
                    int vitri_IN_KIM = dv.Find(new object[] { "IN KIM" });
                    int vitri_bixolon = dv.Find(new object[] { "BIXOLON" });
                    int vitri_bixolon100 = dv.Find(new object[] { "BIXOLON100" });
                    if (vitri > -1 || vitri_bixolon > -1 || vitri_bixolon100 > -1)
                    {
                        mh_viewthanhtoan mh = new mh_viewthanhtoan();
                        if (vitri > -1)
                            mh.InBill_Inlai(BH_PHIEU_MA, true, 1, sTieuDe, iPrintOut);
                        else if (vitri_bixolon > -1)
                            mh.InBill_Inlai(BH_PHIEU_MA, true, 2, sTieuDe, iPrintOut);
                        if (vitri_bixolon100 > -1)
                            mh.InBill_Inlai(BH_PHIEU_MA, true, 3, sTieuDe, iPrintOut);


                    }
                    else if (vitri_A5 > -1)
                    {
                        LPV_POS.mh_pos.mh_inbaocao.mh_inA5 mh = new LPV_POS.mh_pos.mh_inbaocao.mh_inA5();
                        mh.f_inhoadon(DM_BAN_MA, BH_PHIEU_MA, -1, true, 1, sTieuDe, sFooter, false);

                    }
                    else if (vitri_bixolon100 > -1)
                    {
                        //LPV_POS.mh_pos.mh_inbaocao.mh_inBixolon100 mh = new LPV_POS.mh_pos.mh_inbaocao.mh_inBixolon100();
                        //mh.f_inhoadon(DM_BAN_MA, BH_PHIEU_MA, -1, true, 1);
                        mh_viewthanhtoan mh = new mh_viewthanhtoan();
                        mh.InBill_Inlai(BH_PHIEU_MA, true, 3, sTieuDe, iPrintOut);
                    }
                    else if (vitri_IN_KIM > -1)
                    {//chua dc
                        mh_in_KIM mh = new mh_in_KIM();
                        mh.f_inhoadon(DM_BAN_MA, BH_PHIEU_MA, -1, true, sTieuDe);
                    }
                    else
                    {
                        if (vitri_A4 > -1)
                        {
                            //if (drMain["DM_LOAIDICHVU_MA"] + "" == "2")
                            //{//neu la HOTEL thi in ra REPORT "IN A4"                                
                            mh_bc_inbill_khachsan frm_bc_inbill_khachsan = new mh_bc_inbill_khachsan(BH_PHIEU_MA);
                            frm_bc_inbill_khachsan.ShowDialog();//20110324
                            //}
                            //else
                            //{
                            //    if (this.In_Share() == false)
                            //    {                                    //neu ko phai la HOTEL thi in BILL
                            //        print_BIEN_NHAN oprint_BAN_HANG = new print_BIEN_NHAN();
                            //        oprint_BAN_HANG.Print_PHIEU_BIENNHAN(BH_PHIEU_MA);
                            //    }
                            //}
                        }
                        else
                        {
                            if (this.In_Share() == false)
                            {                                ////IN Bill
                                print_BIEN_NHAN oprint_BAN_HANG = new print_BIEN_NHAN();
                                oprint_BAN_HANG.Print_PHIEU_BIENNHAN(BH_PHIEU_MA);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string s = ex.Message;
                }
                finally
                {
                    this.Cursor = Cursors.Default;
                }
            }
            else
                new mh_thongbao("Chọn phiếu để in", 2).ShowDialog();
        }

        #region "   Boolean In_Share()"
        Boolean In_Share()
        {
            if (BH_PHIEU_MA != "")
            {
                cpublic ocpublic = new cpublic();
                Boolean bShare_May_In_thanhtoan = ocpublic.Registry_Get("SHARE_ELLIX31") + "" == "True";
                Boolean bIn_Tai_MayKhac = ocpublic.Registry_Get("SHARE_ELLIX32") + "" == "True";
                if (bShare_May_In_thanhtoan && bIn_Tai_MayKhac)
                {
                    DataTable tbBN = new thuvien().SP_SelectDataTable("sp_DM_MAYIN_BIENNHAN", null, null);
                    DataView dv = tbBN.DefaultView;
                    int iPrintOut = 0;
                    if (tbBN.Rows.Count > 0)
                        int.TryParse(tbBN.Rows[0]["PrintOut"] + "", out iPrintOut);

                    string IPMay_Nhan_DuLieu = ocpublic.Registry_Get("SHARE_ELLIX33") + "";
                    int sPort_May_Nhan_DuLieu = 8888;//int.Parse("0" + ocpublic.Registry_Get("SHARE_ELLIX34"));
                    ocpublic.Send_DuLieu_Den_MayInCOM("thanhtoan", "abc", iPrintOut + "\r\n" + BH_PHIEU_MA, IPMay_Nhan_DuLieu, sPort_May_Nhan_DuLieu);
                    return true;
                }
            }
            return false;
        }
        #endregion

        private void cboKhuVuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (bLoadForm)
            {
                DM_KHUVUC_MA = cboKhuVuc._GetselectedID();
                bLoadForm = false;
                cboBan.Text = "";
                this.ShowcboBan();
                bLoadForm = true;
            }
        }

        private void cboBan_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (bLoadForm)
            //{
            //    bLoadForm = false;
            DM_BAN_MA = cboBan._GetselectedID();
            this.ShowData_tbLeft();
            bLoadForm = true;
            //}
        }

        void ShowcboBan()
        {
            string sSQL = "SELECT aa.OBJID, aa.OBJNAME " +
                          "FROM " +
                          "( " +
                            "SELECT  OBJID = NULL, OBJNAME = '  ----------------' " +
                            " UNION " +
                            "SELECT OBJID, OBJNAME " +
                            "FROM DM_BAN " +
                            "WHERE MAKHUVUC LIKE '" + DM_KHUVUC_MA + "' " +
                          ")aa " +
                          "ORDER BY CAST(aa.OBJID AS INT)"
                          ;

            thuvien othuvien = new thuvien();
            DataTable tbBAN = othuvien.SP_SelectDataTable("sp_executesql", new string[] { "@sSQL" }, new object[] { sSQL });
            cboBan.Properties.Items.Clear();
            othuvien.f_showdataoncombox(cboBan, tbBAN, "OBJID", "OBJNAME");
            if (tbBAN.Rows.Count > 0)
                cboBan.SelectedIndex = 0;
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            this.cboBan_SelectedIndexChanged(null, null);
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (gridViewLeft.FocusedRowHandle >= 0)
            {
                if (new cpublic().KiemTra_CoQuyen(this.Name, btnSua_Bill.Name, 1))//20101215
                {
                    //MessageBox.Show(BH_PHIEU_MA);
                    //mh_banhang_CT_Sua omh_banhang_CT_Sua = new mh_banhang_CT_Sua(lbel_tencongty.Text, this.LoadCaLamViec(), false);
                    mh_banhang_CT_Sua2 omh_banhang_CT_Sua = new mh_banhang_CT_Sua2(lbel_tencongty.Text, this.LoadCaLamViec(), -1);
                    omh_banhang_CT_Sua.sBAN_MA = drMain["DM_BAN_MA"] + "";
                    omh_banhang_CT_Sua.sPhieu_Ma = drMain["OBJID"] + "";
                    omh_banhang_CT_Sua.sMaThe_VIP = drMain["CUSTOMER_ID"] + "";
                    omh_banhang_CT_Sua.sKhuVucMa = cboKhuVuc._GetselectedID();

                    //omh_banhang_CT_Sua.bDaVaoCa = bDaVaoCa;
                    omh_banhang_CT_Sua.ShowInTaskbar = false;
                    DialogResult dlg = omh_banhang_CT_Sua.ShowDialog();
                    if (dlg == DialogResult.Cancel)
                        this.cboBan_SelectedIndexChanged(null, null);
                    else
                        this.gridViewLeft_FocusedRowChanged(null, null);
                }
                else
                    MessageBox_LPV.Show("Bạn không có quyền");
            }
        }
        string LoadCaLamViec()
        {
            string sSQL = "";

            //sSQL = "SELECT * FROM DM_DANGNHAP WHERE OBJCODE LIKE '" + Variable.calamviec + "'";
            sSQL = "SELECT * FROM DM_DANGNHAP WHERE USERNAME LIKE '" + Variable.username + "'";

            DataTable tbCalamviec = new thuvien().SP_SelectDataTable("sp_executesql", new string[] { "@sSQL" }, new object[] { sSQL });
            if (tbCalamviec.Rows.Count == 1)
            {
                //lblVaoCa.Text = "Ra\nCa";
                return tbCalamviec.Rows[0]["OBJNAME"] + "";
            }
            else
                return "";
        }
        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            txt_sophieu.Text = new cpublic().loadformso_point(400, 200);
        }

        private void bton_tim_Click(object sender, EventArgs e)
        {
            f_showdataongridleft_byid(txt_sophieu.Text.Trim());
        }

        private void bton_xemtatca_Click(object sender, EventArgs e)
        {
            ShowData_tbLeft_tatca();
        }

        private void bton_xoa_Click(object sender, EventArgs e)
        {

            string id = "";
            DataTable tb_tam = (DataTable)gridControlLeft.DataSource;
            DataTable tb = tb_tam.Copy();

            DataView dv_source = tb_tam.DefaultView;


            if (tb.Rows.Count < 0 || tb == null)
                return;
            DataView dv = tb.DefaultView;
            dv.RowFilter = "FLAG=True";
            tb = dv.ToTable();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                if (i == tb.Rows.Count - 1)
                    id += "'" + tb.Rows[i]["OBJID"] + "'" + "";
                else
                    id += "'" + tb.Rows[i]["OBJID"] + "'" + ",";
            }
            if (id != "")
            {
                thuvien tv = new thuvien();
                int ketqua = tv.SP_Update("sp_Print_InLai_HoaDon", new string[] { "@muti_str_maphieu" }, new object[] { id });
                if (ketqua < 0)
                {
                    new Mod_messagebox().messagebox_pos("Thông báo", "Xóa không thành công", 2);
                }
                else
                {
                    new Mod_messagebox().messagebox_pos("Thông báo", "Xóa thành công", 2);
                    dv_source.RowFilter = "FLAG = False ";
                }

            }


        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (bLoadForm && radioButton1_TienMat.Checked)
            {//radioButton1_TienMat
                bLoadForm = false;
                radioButton2_The.Checked = false;
                radioButton1_TienMat.Checked = true;
                this.Update_PhuongThucThanhToan();
                bLoadForm = true;
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (bLoadForm && radioButton2_The.Checked)
            { //radioButton2_The
                bLoadForm = false;
                radioButton2_The.Checked = true;
                radioButton1_TienMat.Checked = false;
                this.Update_PhuongThucThanhToan();
                bLoadForm = true;
            }
        }
        void Update_PhuongThucThanhToan()
        {
            thuvien othuvien = new thuvien();
            string sSQL = "";
            if (radioButton2_The.Checked)
                sSQL = "UPDATE BH_PHIEU SET TINH_TIEN_BANG_THE = 1 WHERE OBJID = '" + drMain["OBJID"] + "'";
            else if (radioButton1_TienMat.Checked)
                sSQL = "UPDATE BH_PHIEU SET TINH_TIEN_BANG_THE = 0 WHERE OBJID = '" + drMain["OBJID"] + "'";

            if (sSQL != "")
                othuvien.SP_Update("sp_executeSQL",
                                    new string[] { "@sSQL" },
                                    new object[] { sSQL });

            if (radioButton2_The.Checked)
                MessageBox_LPV.Show("THANH TOÁN BẰNG THẺ");
            else if (radioButton1_TienMat.Checked)
                MessageBox_LPV.Show("THANH TOÁN BẰNG TIỀN MẶT");

        }

        private void gridViewLeft_GotFocus(object sender, EventArgs e)
        {
            if (gridViewLeft.RowCount == 1)
            {

                this.gridViewLeft_FocusedRowChanged(null, null);

            }
        }

        //ANHLQ
        private void BtnNV_PhucVu_Click(object sender, EventArgs e)
        {
            //ANHLQ
            DataRow row = gridViewLeft.GetDataRow(gridViewLeft.FocusedRowHandle);
            if (row != null)
            {
                frmDM_NHANVIEN_PHUCVU frm = new frmDM_NHANVIEN_PHUCVU(row);
                frm.ShowDialog();
            }
        }

        private void BtnNguoiGioiThieu_Click(object sender, EventArgs e)
        {
            //ANHLQ
            DataRow row = gridViewLeft.GetDataRow(gridViewLeft.FocusedRowHandle);
            if (row != null)
            {
                mh_banhang_nguoigioithieu frm = new mh_banhang_nguoigioithieu(row);
                frm.ShowDialog();
            }
        }
    }
}
